package cn.edu.buaa.treehole.service;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

@Service
public class NameService {
    private ArrayList<String> names;
    private final static String[] nameArray = {
            "Host",
            "Ampere",
            "Bolzano",
            "Cauchy",
            "Darwin",
            "Emily",
            "Frank",
            "Gauss",
            "Harrison",
            "Isaac",
            "Joseph",
            "Kennedy",
            "Luke",
            "Marshal",
            "Nancy",
            "Olivia",
            "Paul",
            "Quentin",
            "Richard",
            "Steve",
            "Tomas",
            "Ulysses",
            "Victor",
            "Warren",
            "Zack",
            "Angel",
            "Benjamin"
    };

    public String getName(int order) {
        if (names == null) {
            synchronized (NameService.class) {
                if (names == null) {
                    /*try {
                        FileReader reader = new FileReader("name.list");
                        Scanner scanner = new Scanner(reader);*/
                        ArrayList<String> tmp = new ArrayList<>(Arrays.asList(nameArray));
                        /*while (scanner.hasNextLine()) {
                            tmp.add(scanner.nextLine().strip());
                        }*/
                        names = tmp;
                    /*} catch (FileNotFoundException e) {
                        String msg = "name list initialize wrong";
                        LoggerFactory.getLogger(NameService.class).error(msg, e);
                        throw new RuntimeException(msg);
                    }*/
                }
            }
        }
        return names.get(order);
    }
}
